import { ref } from 'vue'
import { getOrderList as getOrderListApi } from '@/api/order'

/**
 * 工单列表服务
 */
export default function () {
  const getOrderListLoading = ref(false)
  const getOrderListParam = ref<OrderAPIRequest.GetList>({
    agingFiltrate: '',
    buyPlatformOrderNo: '',
    childStatus: '',
    dateFieldType: '',
    dateType: '',
    departmentId: '',
    empIds: [],
    empType: '',
    engineer: '',
    fieldId: '',
    fieldSearch: '',
    goodsCode: '',
    goodsSearch: '',
    limit: 0,
    maxServiceTime: '',
    minServiceTime: '',
    moduleIds: [],
    networkIds: [],
    networkType: '',
    orderEndTime: '',
    orderNo: '',
    orderNodeTimeoutType: '',
    orderStartTime: '',
    orderStatus: [],
    orderUserSearch: '',
    otherOrderNo: '',
    page: 0,
    sendForRepairPhone: '',
    sendForRepairUsername: '',
    serviceTimeType: '',
    snCode: '',
    sources: [],
    sql: '',
    status: '',
    userArea: '',
    userCity: '',
    userProvince: '',
    workLevelIds: [],
    workSourceIds: [],
  })
  const orderList = ref<Array<OrderListItem>>([])
  const orderListTotal = ref(0)

  const resetGetOrderListParam = () => {
    getOrderListParam.value = {
      agingFiltrate: '',
      buyPlatformOrderNo: '',
      childStatus: '',
      dateFieldType: '',
      dateType: '',
      departmentId: '',
      empIds: [],
      empType: '',
      engineer: '',
      fieldId: '',
      fieldSearch: '',
      goodsCode: '',
      goodsSearch: '',
      limit: 0,
      maxServiceTime: '',
      minServiceTime: '',
      moduleIds: [],
      networkIds: [],
      networkType: '',
      orderEndTime: '',
      orderNo: '',
      orderNodeTimeoutType: '',
      orderStartTime: '',
      orderStatus: [],
      orderUserSearch: '',
      otherOrderNo: '',
      page: 0,
      sendForRepairPhone: '',
      sendForRepairUsername: '',
      serviceTimeType: '',
      snCode: '',
      sources: [],
      sql: '',
      status: '',
      userArea: '',
      userCity: '',
      userProvince: '',
      workLevelIds: [],
      workSourceIds: [],
    }
  }

  const getOrderList = async () => {
    try {
      getOrderListLoading.value = true
      const res = await getOrderListApi(getOrderListParam.value)
      getOrderListLoading.value = false
      if (res.data.code === 20000) {
        orderListTotal.value = Number(res.data.data.total)
        orderList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getOrderListLoading.value = false
      return '获取工单列表失败，请稍后重试'
    }
  }

  return {
    orderList,
    orderListTotal,
    getOrderListParam,
    getOrderListLoading,
    getOrderList,
    resetGetOrderListParam,
  }
}
